$(function() {
	$('.order-all').click(function () {
		$('.order-type').addClass('show-order');
	});
	$('.close').click(function () {
		$('.order-type').removeClass('show-order');
	});

	$('#pj-close').click(function(){
		$('#order-comment').hide();
	});

	$('.pinjia').click(function () {
		$('.pop-box').css('display', 'block');
		var val = $(this).data('id');
		var order_id = $(this).data('order_id');
		$('.rstid').val(val);
		$('.order_id').val(order_id);
	});

	$('.star').click(function () {
		$(this).removeClass('grey-star');
		$(this).prevAll().removeClass('grey-star');
		$(this).nextAll().addClass('grey-star');
	});

	//评价订单
	$(document).on('click','.pj-btn', function () {
		var score1 = $('.star1').find(".grey-star").length;
		var score2 = $('.star2').find(".grey-star").length;
		var score3 = $('.star3').find(".grey-star").length;
		var id = $(".rstid").val();
		var comment = $('#comment').val();
		var order_id = $(".order_id").val();
		var res_id = $('#res_id').val();
		if(score1 == 5){
			$.alert('error','请评价口味');
			return false;
		}

		if(score2 == 5){
			$.alert('error','请评价质量');
			return false;
		}
		if(score3 == 5){
			$.alert('error','请评价配送');
			return false;
		}

		var data = {};
		var json = {};
		data.score = 5-score1;
		data.score_send = 5-score3;
		data.score_zl = 5-score2;
		data.comment = comment;
		data.id = id;
		data.order_id = order_id;
		json.data = data;
		json.url = '/restaurant/ZduMobile/comment?res_id='+res_id;
		json.method = 'post';
		json.type = 'json';
		$.getAjax(json, true, reload, '');
		$('.pop-box').css('display', 'none');
	});

	//取消订单
	$(document).on('click', '.reset', function () {
		var order_id = $(this).data('order_id');
		var res_id = $('#res_id').val();
		$.confirm("确定取消该订单吗？", drop);
		function drop() {
			var data = {};
			var json = {};
			data.order_id = order_id;
			json.data = data;
			json.url = '/restaurant/ZduMobile/resetOrder?res_id='+res_id;
			json.method = 'post';
			json.type = 'json';
			$.getAjax(json, true, reload, '');
		}
	});

	//确认收货
	$(document).on('click', '.sure-btn', function () {
		var order_id = $(this).data('order_id');
		var res_id = $('#res_id').val();
		$.confirm("确认收货吗？", drop);
		function drop() {
			var data = {};
			var json = {};
			data.order_id = order_id;
			json.data = data;
			json.url = '/restaurant/ZduMobile/sureOrder?res_id='+res_id;
			json.method = 'post';
			json.type = 'json';
			$.getAjax(json, true, reload, '');
		}
	});

	//提交订单
	$(document).on('click','.pay-btn',function(){

		var username = $('.username').val();
		var mobile = $('.mobile').val();

		var restaurant_id = $('.restaurant_id').val();
		var memo = $('#memo').val();
		var order_id = $(this).data('order_id');
		var res_id = $('#res_id').val();
		if ($(".weipay_check").length <= 0) {
			$.alert("error","请选择支付方式");
			return false;
		}

		var pay_type = $(".weipay_check").data("type");
		if(pay_type == 4){
			var campuscard_bind = $("#campuscard_bind").val();
			if(campuscard_bind != 1){
				var bind_url=$('#bind_url').val();
				$.alert('error', '校园卡未绑定');
				window.sessionStorage.setItem("local_url", bind_url);
				setTimeout(redirect, 2000);
				return false;
			}
		}
		var info = {};
		var json = {};
		info.order_id = order_id;
		info.memo = memo;
		info.restaurant_id = restaurant_id;
		info.pay_type = pay_type;
		storedPay();
		/*if(pay_type == 4) {
			$.confirm("是否确认使用校园卡余额付款？", storedPay);
		} else {
			storedPay();
		}*/

		function storedPay() {
			$.ajax({
				'url': "/restaurant/ZduMobile/orderPay?res_id="+res_id,
				'dataType':'json',
				'type': 'POST',
				'data': info,
				'async': false,
				success: function(data) {
					if (data.status == 'WS') {
						var wxpay = $.parseJSON(data.url);
						pay(wxpay);
					} else if(data.status == 'BS'){
						$.alert('success','付款成功');
						window.sessionStorage.setItem("local_url",  "/restaurant/ZduMobile/myOrder/res_id/"+res_id);
						setTimeout(redirect, 2000);
					} else if (data.status == 'CampusCard') { //校园卡支付
						$("input[name=tranamt]").val(data.data['tranamt']);
						$("input[name=account]").val(data.data['account']);
						$("input[name=sno]").val(data.data['sno']);
						$("input[name=toaccount]").val(data.data['toaccount']);
						$("input[name=thirdsystem]").val(data.data['thirdsystem']);
						$("input[name=thirdorderid]").val(data.data['thirdorderid']);
						$("input[name=ordertype]").val(data.data['ordertype']);
						$("input[name=sign]").val(data.data['sign']);
						$("input[name=orderdesc]").val(data.data['orderdesc']);
						$("input[name=praram1]").val(data.data['praram1']);
						$("input[name=thirdurl]").val(data.data['thirdurl']);
						$("input[name=errorurl]").val(data.data['errorurl']);
						$("#form_campuscard").attr("action",data.data['interface_url']);
						$("#form_campuscard").submit();
					} else {
						$.alert('error',data.message);
						return false;
					}
				}
			});
		}

	});


	//微支付
	function pay(wxpay){
		var res_id = $('#res_id').val();
		if (typeof WeixinJSBridge == "undefined"){
			if( document.addEventListener ){
				document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			}else if (document.attachEvent){
				document.attachEvent('WeixinJSBridgeReady', jsApiCall);
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			}
		}else{
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				wxpay,
				function(res){
					WeixinJSBridge.log(res.err_msg);
					if(res.err_msg == "get_brand_wcpay_request:ok"){
						paySuccess();
					}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
						$.alert('error','付款已取消');
						setTimeout(location.href = "/restaurant/ZduMobile/myorder/res_id/"+res_id,2000);
					}else{
						$.alert('error','调用微支付失败');
						return false;
					}
				}
			);
		}
	}

	function paySuccess(){
		var res_id = $('#res_id').val();
		location.href = "/restaurant/ZduMobile/myOrder/res_id/"+res_id;
	}

	function redirect() {
		location.href = window.sessionStorage.getItem("local_url");
		window.sessionStorage.setItem("local_url",null);
	}

	//页面刷新
	function reload() {
		setTimeout('location.reload()', 2000);
	}
});